package com.example.demoserver.controller;

import com.example.demoserver.entity.InteGoods;
import com.example.demoserver.entity.InteSaleInfo;
import com.example.demoserver.entity.InteSaleorder;
import com.example.demoserver.service.InteLogisticsService;
import com.example.demoserver.service.InteSaleorderService;
import com.example.demoserver.service.impl.InteGoodsServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/sale")
@RequiredArgsConstructor
@Component
public class InteSaleorderController {
    private final InteSaleorderService inteSaleorderService;
    private final InteLogisticsService inteLogisticsService;
    private final InteGoodsServiceImpl inteGoodsService;
    @GetMapping("/selectall")
    public List<InteSaleorder> selectAll(){
        List<InteSaleorder> list=inteSaleorderService.selectAll();
        return list;
    }
    @GetMapping("/selectsale")
    public List<InteSaleInfo> selectSale(){
        List<InteSaleorder> list=inteSaleorderService.selectAll();
        List<InteSaleInfo> list1=new ArrayList<>();
        for (InteSaleorder i:list) {
            InteSaleInfo inteSaleInfo=new InteSaleInfo();
            InteGoods inteGoods=inteGoodsService.selectById((int)i.getGoodsid());
            inteSaleInfo.setInteSaleorder(i);
            inteSaleInfo.setGoodsname(inteGoods.getGoodsname());
            list1.add(inteSaleInfo);
        }
        return list1;
    }

    @GetMapping("/selectproduct")
    public List<InteSaleorder> selectProduct(){
        List<InteSaleorder> list=inteSaleorderService.selectProduct();
        return list;
    }

    @GetMapping("/setstart")
    public InteSaleorder setStartProduct(Integer id){
        InteSaleorder inteSaleorder=new InteSaleorder();
        inteSaleorder.setId(id);
        inteSaleorder.setOrderstate(2);
        inteSaleorder.setProstartime(Util.getNowTime());
        boolean bol=inteSaleorderService.updateById(inteSaleorder);
        if(bol){
            doProduct(inteSaleorderService.selectById(id));
            return inteSaleorder;
        }

        return null;
    }
    public void doProduct( InteSaleorder inteSaleorder){
        ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
        executor.schedule(new Runnable() {
            public void run() {
                inteSaleorder.setProendtime(Util.getNowTime());
                inteSaleorder.setOrderstate(3);
                inteSaleorderService.updateById(inteSaleorder);
            }
        }, 60*(int)(inteSaleorder.getGoodsnum()), TimeUnit.SECONDS);

        executor.shutdown();
    }


    @GetMapping("/selectproduced")
    public List<InteSaleorder> selectProduced(){
        List<InteSaleorder> list=inteSaleorderService.selectProduced();
//        List<InteSaleorder> list1=new ArrayList<>();
//        for (InteSaleorder i:list) {
//            if(inteLogisticsService.selectBySaleId((Integer) i.getId())==null){
//                list1.add(i);
//            }
//        }
        return list;
    }

    @PostMapping("/insert")
    public InteSaleorder goodsInsert(@RequestBody InteSaleorder inteSaleorder){
        inteSaleorder.setOrdertime(Util.getNowTime());
        inteSaleorder.setOrderstate(0);
        boolean bol=inteSaleorderService.save(inteSaleorder);
        if(bol) return inteSaleorder;
        return null;
    }

    @PostMapping("/update")
    public InteSaleorder goodsUpdate(@RequestBody InteSaleorder inteSaleorder){
        boolean bol=inteSaleorderService.updateById(inteSaleorder);
        if(bol) return inteSaleorder;
        return null;
    }

    @GetMapping("/delete")
    public Integer goodsDelete(Integer id){
        return inteSaleorderService.deleteById(id);
    }

    @GetMapping("/setstate")
    public boolean setOrderState(Integer id,Integer state){
        InteSaleorder inteSaleorder=new InteSaleorder();
        inteSaleorder.setId(id);
        inteSaleorder.setOrderstate(state);
        return inteSaleorderService.updateById(inteSaleorder);
    }

    @GetMapping("/overallproduction")
    public List<Map<Object,Object>> overallProduction(){
        List<InteSaleorder> list1=inteSaleorderService.selectSome(1,null);
        List<InteSaleorder> list2=inteSaleorderService.selectSome(2,null);
        List<InteSaleorder> list3=inteSaleorderService.selectSome(3,null);
        List<Map<Object,Object>> list=new ArrayList<>();
        list.add(setMap(list1.size(),"未生产"));
        list.add(setMap(list2.size(),"生产中"));
        list.add(setMap(list3.size(),"已生产"));
        return list;
    }
    @GetMapping("/todayproduction")
    public List<Map<Object,Object>> todayProduction(){
        List<InteSaleorder> list1=inteSaleorderService.selectSome(1,1);
        List<InteSaleorder> list2=inteSaleorderService.selectSome(2,1);
        List<InteSaleorder> list3=inteSaleorderService.selectSome(3,1);
        List<Map<Object,Object>> list=new ArrayList<>();
        list.add(setMap(list1.size(),"未生产"));
        list.add(setMap(list2.size(),"生产中"));
        list.add(setMap(list3.size(),"已生产"));
        return list;
    }

    @GetMapping("/sevendayproduction")
    public Map<Object,Object> sevenDayProduction(){
        List<List<InteSaleorder>> list=inteSaleorderService.sevenDayProduction();
        Map<Object,Object> map=new HashMap<>();
        List<Integer> value=new ArrayList<>();
        List<String> name=new ArrayList<>();
        for (List<InteSaleorder> i:list) {
            value.add(i.size());
        }
        Collections.reverse(value);
        map.put("datax",getYearDay());
        map.put("datay",value);
        return map;
    }


    public List<String> getYearDay(){

        List<String> dates = new ArrayList<>();

        // 获取今天的日期
        LocalDate today = LocalDate.now();

        // 将过去七天的日期（包括今天）添加到列表中
        for (int i = 1; i < 8; i++) {
            dates.add(today.minusDays(i).toString());
        }

        // 对列表进行升序排序
        Collections.sort(dates);
        return dates;
    }
    public Map<Object,Object> setMap(Integer value,String name){
        Map<Object,Object> map=new HashMap<>();
        map.put("value",value);
        map.put("name",name);
        return  map;
    }
}
